package com.xiaomi.eco.config;

import org.springframework.core.NamedThreadLocal;

public final class UserContext {

    private static final ThreadLocal<LoginInfo> currentUser = new NamedThreadLocal<LoginInfo>("Current User");


    public static LoginInfo currentUser() {
        LoginInfo loginInfo = currentUser.get();
        if (loginInfo == null) {
            System.out.println("The current user is not set");
            return null;
        }
        return loginInfo;
    }


    public static void setCurrentUser(LoginInfo loginInfo) {
        if (loginInfo != null) {
            currentUser.set(loginInfo);
        }
    }


    public static void removeCurrentUser() {
        currentUser.remove();
    }


    public static String currentUserCode() {
        LoginInfo loginInfo = UserContext.currentUser();
        if (loginInfo == null) {
            return "";
        }
        return loginInfo.getUserCode();
    }

}
